﻿using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web;

namespace Dng.TraficTracking
{
	[Guid("4657177d-1a11-4539-b53d-f3051cefeb72")]
	public class TraficTracking : System.Web.UI.WebControls.WebParts.WebPart
	{
		private string _Victim = string.Empty;
		private ViewMode _ViewMode = ViewMode.VisitedPage;

		protected override void CreateChildControls()
		{
			Label label;
			base.CreateChildControls();
			if (this.ViewModeReport == ViewMode.VisitedPage)
			{
				HttpRequest request = HttpContext.Current.Request;
				string url = Global.Instance.CompactUrl(request);
				if (!this.Victim.Equals(string.Empty))
				{
					url = this.Victim;
				}
				if (url.Length >= 1)
				{
					SPSite site = new SPSite(Global.Instance.SiteUrl);
					SPList supportList = site.OpenWeb().Lists[Global.Instance.TracficTrackingSummaryListName];
					string query = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + url + "</Value></Eq></Where>";
					SPQuery spquery = new SPQuery
					{
						Query = query
					};
					SPListItemCollection lists = supportList.GetItems(spquery);
					label = new Label();
					if (lists.Count < 1)
					{
						label.Text = "1";
					}
					else
					{
						label.Text = "Lượt truy cập: " + Convert.ToInt32(lists[0]["Visited"]).ToString("#,##0");
					}
					this.Controls.Add(label);
				}
			}
			else
			{
				long count;
				if (this.ViewModeReport == ViewMode.TotalHit)
				{
					count = Global.Instance.CurrentVisited;
					try
					{
						count = Convert.ToInt64((HttpContext.Current.Application["HitCounter"] == null) ? "0" : HttpContext.Current.Application["HitCounter"].ToString()) + count;
					}
					catch
					{
					}
					label = new Label
					{
						Text = "Lượt truy cập: " + Math.Round((decimal)(count * Global.Instance.CountRate)).ToString("#,##0")
					};
					this.Controls.Add(label);
				}
				else if (this.ViewModeReport == ViewMode.Tracking)
				{
					string script = "\r\n<div style='display:none'>\r\n<iframe id='clockRequestTarget'></iframe>\r\n</div>\r\n<script type='text/javascript'>\r\nvar handler=self.setInterval('clock()'," + (Global.Instance.RefreshTracking * 10000.0) + ");\r\nfunction clock()\r\n{\r\n    document.getElementById('clockRequestTarget').src='Refresh.aspx?salt='+Math.random();\r\n}\r\n</script>\r\n";
					HtmlGenericControl c = new HtmlGenericControl
					{
						InnerHtml = script
					};
					this.Controls.Add(c);
				}
				else
				{
					count = Global.Instance.CurrentVisited;
					try
					{
						count = Convert.ToInt64((HttpContext.Current.Application["Counter"] == null) ? "0" : HttpContext.Current.Application["Counter"].ToString()) + count;
					}
					catch
					{
					}
					label = new Label
					{
						Text = "Lượt truy cập: " + Math.Round((decimal)(count * Global.Instance.CountRate)).ToString("#,##0")
					};
					this.Controls.Add(label);
				}
			}
		}

		public string Victim
		{
			get
			{
				return this._Victim;
			}
			set
			{
				this._Victim = value;
			}
		}

		public ViewMode ViewModeReport
		{
			get
			{
				return this._ViewMode;
			}
			set
			{
				this._ViewMode = value;
			}
		}
	}
}
